<?php
namespace Modules\Product\Entities;

use Modules\Order\Traits\Skuable;
use Modules\Core\Entities\BaseEntity as Model;

use Modules\Asset\Entities\PackageSku;

use ThrowException;

class Sku extends Model
{
	use Skuable;

    protected $guarded = [];
    protected $table = 'skus';

    static public $resourceName = 'sku';

    protected $casts = [
        'attrs' => 'json',
    ];

    protected $appends = ['title'];

    public function getType($type) 
    {
        $map = [
            'base_sku_type' => 'product_sku',
            'node_type' => 'product'
        ];

        return @$map[$type];
    }

    public function getNodeEntity() 
    {
        return $this->product;
    }

    public function parent_product()
    {
        return $this->belongsTo('\Modules\Product\Entities\Product', 'product_id');
    }

    public function product()
    {
        return $this->belongsTo('\Modules\Product\Entities\Product', 'product_id');
    }

    // 可以兑换此sku的碎片
    public function chip()
    {
        return $this->belongsTo('\Modules\Chip\Entities\Chip', 'cover_chip_id');
    }

    // 获得供应商
    public function getSupplier()
    {
        return $this->product ? $this->product->supplier : NULL;
    }

    public function getThumbAttribute($value)
    {
        return $value ?: $this->product->thumb;
    }

    public function getTitleAttribute($value)
    {
        return $this->product->title;
    }

    // 返回商品类型
    public function getBaseSkuType()
    {
        return 'product';
    }

    public function updateSalesSync()
    {
        $this->product->updateSalesAndStock();
    }

    // public function updateStock($total)
    // {
    //     $this->update([
    //         'stock' => $total
    //     ]);
    // }

    // 赠送成一个package sku
    public function sendAsPackageSku($user)
    {
        $order = NULL;
        $sku = $this;
        $product = $this->product;
        $info = [
            'uuid' => uniqid(),
            'user_id' => $user->id,
            'title' => $sku->title,
            'number' => $sku->number,
            'thumb' => $sku->thumb,
            'sku_type' => 2,  // 自定义实物
            'product_id' => $sku->product_id,
            'sku_id' => $sku->id,
            // 'is_free_order' => 0,
            'assets' => $sku->assets,
            'total' => 1,
            'is_special_sku' => 0,
            'score_price' => $sku->getSellingPrice('score'),
            'money_price' => $sku->getSellingPrice('money'),
            'pay_order_id' => @$order->id,
            'activity_id' => NULL,
            'activity_type' => NULL,
            'activity_uuid' => NULL,
            'node_title' => $product->title,
            'node_money_price' => $product->money_price,
            'node_score_price' => $product->score_price,
            'node_id' => $product->id,
            'node_type' => $product->getType('node_type'),
            'node_uuid' => $product->uuid,
            'open_indexs' => NULL,
            'open_index' => NULL,
            'options' => NULL,
            'source_type' => 4,     // 兑换码
            'presell_date'  => NULL,
            'room_id' => NULL,
            'sku_id' => $sku->id,
            'use_lucky_score' => 0,
            'is_return_saleable' => 0
        ];

        return PackageSku::create($info);
    }

}
